package org.wit.ecommerce.action.admin;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.wit.ecommerce.domain.Discount;
import org.wit.ecommerce.dto.Navigator;
import org.wit.ecommerce.service.IDiscountService;

import com.opensymphony.xwork2.ActionSupport;
/**
 * @author Justin
 *
 */
@SuppressWarnings("serial")
@Namespace("/admin/discount")
public class DiscountAction extends ActionSupport {
	@Autowired
	private IDiscountService discountService;
	private int page = 1;
	private Navigator<Discount> discounts;
	private Long id;
	private Discount discount;
	
	@Action("list")
	@Override
	public String execute(){
		discounts = discountService.getDiscountsByPage(page, null);
		return SUCCESS;
	}
	
	@Action("edit")
	public String edit(){
		discount = discountService.getDiscountById(id);
		return SUCCESS;
	}
	
	@Action(value="update", results={@Result(name="success", type="redirectAction", location="list")})
	public String update(){
		discountService.updateDiscount(discount);
		return SUCCESS;
	}
	
	@Action(value="delete", results={@Result(name="success", type="redirectAction", location="list")})
	public String delete(){
		discountService.deleteDiscount(id);
		return SUCCESS;
	}
	
	@Action(value="add", results={@Result(name="success", location="/WEB-INF/jsp/admin/discount/edit.jsp")})
	public String add(){
		return SUCCESS;
	}

	public void setDiscountService(IDiscountService discountService) {
		this.discountService = discountService;
	}

	public Navigator<Discount> getDiscounts() {
		return discounts;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Discount getDiscount() {
		return discount;
	}

	public void setDiscount(Discount discount) {
		this.discount = discount;
	}
}
